/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.shishuo.cms.action;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.shishuo.cms.constant.FolderConstant;
import com.shishuo.cms.entity.Article;
import com.shishuo.cms.entity.Headline;
import com.shishuo.cms.entity.vo.ArticleVo;
import com.shishuo.cms.entity.vo.FolderVo;
import com.shishuo.cms.entity.vo.HeadlineVo;
import com.shishuo.cms.exception.TemplateNotFoundException;
import com.shishuo.cms.service.ArticleService;
import com.shishuo.cms.service.FolderService;
import com.shishuo.cms.service.HeadlineService;
import com.shishuo.cms.util.HttpUtils;

/**
 * 首页
 * 
 * @author Herbert
 */
@Controller
public class IndexAction extends BaseAction {
	
	@Autowired
	private ArticleService articleService;
	@Autowired
	private FolderService folderService;
	@Autowired
	private HeadlineService headlineService;
	/**
	 * 首页
	 * 
	 * @param pageNum
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String defalut(
			@RequestParam(value = "p", defaultValue = "1") long p,HttpServletRequest request,
			ModelMap modelMap) {
		return home(p, modelMap,request);
	}

	/**
	 * 首页
	 * 
	 * @param pageNum
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public String home(@RequestParam(value = "p", defaultValue = "1") long p,
			ModelMap modelMap,HttpServletRequest request) {
		try {
			modelMap.addAttribute("p", p);
			modelMap.addAttribute("g_folderId", 0);
			List<FolderVo> folderList = folderService.getFolderListByFatherId(0,//获取0->顶级分类下的子类别 (显示状态)
					FolderConstant.status.display);
			List<ArticleVo> articleXwList = articleService.getArticleListOfNumByFolderEnameDisplay("xwzx",3);
			List<ArticleVo> articleCpList = articleService.getArticleListOfNumByFolderEnameDisplay("cpjs",6);
			List<ArticleVo> leftRollBarList=articleService.getArticleListOfNumByFolderEname("indexleft",4);
//			List<ArticleVo> footInfoBarList=articleService.getArticleListOfNumByFolderEname("footinfo",100);
//			dealArticleContent(leftRollBarList,HttpUtils.getContextPath(request));
			List<HeadlineVo> headlineList = headlineService.getHeadlineList();
			
			modelMap.addAttribute("folderList", folderList);
			modelMap.addAttribute("articleXwList", articleXwList);
			modelMap.addAttribute("articleCpList", articleCpList);
			modelMap.addAttribute("leftRollBarList", leftRollBarList);
//			modelMap.addAttribute("footInfoBarList", footInfoBarList);
			modelMap.addAttribute("headlineList", headlineList);
			
			return themeService.getDefaultTemplate();
		} catch (TemplateNotFoundException e) {
			modelMap.addAttribute("g_folderId", 0);
			logger.fatal(e.getMessage());
			return themeService.get404();
		}
	}

	/**
	 * 404
	 * 
	 * @return
	 */
	@RequestMapping(value = "/404.htm", method = RequestMethod.GET)
	public String pageNotFound(ModelMap modelMap) {
		modelMap.addAttribute("g_folderId", 0);
		return themeService.get404();
	}

	/**
	 * 500
	 * 
	 * @return
	 */
	@RequestMapping(value = "/500.htm", method = RequestMethod.GET)
	public String error(ModelMap modelMap) {
		modelMap.addAttribute("g_folderId", 0);
		return themeService.get500();
	}
//	private void dealArticleContent(List<ArticleVo> articleList,String path){
//		java.util.regex.Pattern picureAddrRegex = java.util.regex.Pattern.compile("src=\"([\\S]+?)\"");//匹配图像地址
//		java.util.regex.Pattern picureAltRegex = java.util.regex.Pattern.compile("alt=\"([\\S]+?)\"");//匹配图像地址
		
//		for(ArticleVo one:articleList)
//		{
//			one.setContent(one.getContent().replaceAll("<p[\\s\\S]*?>", "").replaceAll("</p>", ""));
//			if(!one.getContent().contains("</video>") && !one.getContent().contains("<embed"))//1.上传视频 2.视频外链接
//			{
//				Matcher m1=picureAddrRegex.matcher(one.getContent());
//				if(m1.find())
//					one.setContent(m1.group(1));
//			}
//			else
//			{
//				/*
//				 * 
//<p>  HTML示例代码
//<img src="/upload/image/20150617/1434507678777018883.jpg" title="1434507678777018883.jpg" alt="imgbanner01.jpg"/>
//<video class="edui-upload-video  vjs-default-skin video-js" controls="" preload="none" width="600" height="350" src="/upload/video/20150617/1434507645367049191.mp4" data-setup="{}">
//<source src="/upload/video/20150617/1434507645367049191.mp4" type="video/mp4"/></video>
//</p>
//				 * */
//				if(one.getContent().contains("</video>") )
//				{
//				String temp[]=one.getContent().split("<video");
//				String bigPictureSrc=temp.length==2?temp[0]:"";
//				String vedioHtml=temp.length==2?temp[1]:temp[0];
//				vedioHtml="<video"+vedioHtml;
//				Matcher m1=picureAddrRegex.matcher(bigPictureSrc);
//				if(m1.find())
//				one.setContent(m1.group(1));//大图（预览图）的html
//				one.setVideoHtml(vedioHtml.replaceAll("/upload", path+"/upload"));//视频的html
////				System.out.println(one.getContent()+"------------------"+one.getVideoHtml());
//				}
//				else if(one.getContent().contains("<embed") )
//				{
//					String temp[]=one.getContent().split("<embed");
//					String bigPictureSrc=temp.length==2?temp[0]:"";
//					String vedioHtml=temp.length==2?temp[1]:temp[0];
//					vedioHtml="<embed"+vedioHtml;
//					Matcher m1=picureAddrRegex.matcher(bigPictureSrc);
//					if(m1.find())
//					one.setContent(m1.group(1));//大图（预览图）的html
//					one.setVideoHtml(vedioHtml);//视频的html
//				}
//			}
//		}
}
